<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>数据表格的重载 - 数据表格</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/style/admin.css" media="all">
    <link rel="stylesheet" href="${global_templets_skin}/systemstyle/layui/treetable-lay/treetable.css" media="all">
    <style>
        .search {
            height: 36px;
            line-height: 36px;
            padding-left: 36px;
        }
    </style>
</head>
<body layadmin-themealias="default">

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>组件</cite></a>
        <a><cite>数据表格</cite></a>
        <a><cite>数据表格的重载</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto"
             style="padding-top: 10px; padding-bottom: 10px;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">发帖人</label>
                    <div class="layui-input-block">
                        <input type="text" name="poster" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">发帖内容</label>
                    <div class="layui-input-block">
                        <input type="text" name="content" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">帖子状态</label>
                    <div class="layui-input-block">
                        <select name="top">
                            <option value="0">正常</option>
                            <option value="1">置顶</option>
                            <option value="2">封禁</option>
                        </select>
                        <div class="layui-unselect layui-form-select">
                            <div class="layui-select-title"><input type="text" placeholder="请选择" value="正常" readonly=""
                                                                   class="layui-input layui-unselect"><i
                                    class="layui-edge"></i></div>
                            <dl class="layui-anim layui-anim-upbit">
                                <dd lay-value="0" class="layui-this">正常</dd>
                                <dd lay-value="1" class="">置顶</dd>
                                <dd lay-value="2" class="">封禁</dd>
                            </dl>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-forum-list search" lay-submit=""
                            lay-filter="LAY-app-forumlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>


        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>
                <a class="layui-btn layuiadmin-btn-admin" lay-href="/admin/menu/add_user_menu.do" lay-text="添加用户菜单">添加</a>
            </div>
            <table class="layui-hide" id="menu" lay-filter="menu"></table>

            <script type="text/html" id="test-table-switchTpl">
                <!-- 这里的 checked 的状态只是演示 -->
                <input type="checkbox" name="sex" lay-skin="switch" lay-text="隐藏|显示" lay-filter="test-table-sexDemo"
                       value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.id== 10003
                       ? '' : 'checked' }}>
            </script>
            <blockquote class="layui-elem-quote">此处由于是静态模拟数据，所以搜索后重载的结果没变，这并非是 BUG。实际使用时改成真实接口并根据搜索的字段筛选出对应的数据即可。
            </blockquote>

        </div>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="add">新增</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="${global_templets_skin}/systemstyle/layui/layui.js"></script>
<script>
    layui.config({
        base: '${global_templets_skin}/systemstyle/'
    }).extend({
        treetable: 'layui/treetable-lay/treetable',
    });

    layui.use(['treetable', 'table', 'layer', 'form'], function () {
        var table = layui.table
            , layer = layui.layer
            , form = layui.form
            , $ = layui.$
            , treetable = layui.treetable;
        //渲染表格
        var renderTable = function () {
            layer.load(2);
            treetable.render({
                id: 'menu',
                treeColIndex: 1,	//树形图标显示在第几列
                treeSpid: '0',		//最上级的父级id
                treeIdName: 'id',	//id字段的名称
                treePidName: 'parentId',	//pid字段的名称，父级菜单id
                treeDefaultClose: true,	//是否默认折叠
                treeLinkage: false,		//父级展开时是否自动展开所有子级
                elem: '#menu',	//表格id
                url: '/admin/menu/user_listData.do',
                //toolbar: '#toolbarDemo',
                page: false,
                cellMinWidth: 120,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'menuName', title: '名称', width: 180},
                    {field: 'url', title: '地址', width: 180},
                    {field: 'perms', title: '权限', width: 200},
                    {
                        field: 'menuType', title: '类型', width: 150,
                        templet: function (d) {
                            if (d.menuType == "M") {
                                return '目录';
                            } else if (d.menuType == "C") {
                                return '菜单';
                            } else if (d.menuType == "F") {
                                return '按钮';
                            }
                        }
                    },
                    {field: 'visible', title: '显示', width: 85, templet: '#test-table-switchTpl', unresize: true},
                    {field: 'orderNum', title: '排序', width: 120},
                    {fixed: 'right', align: 'center', title: '操作', toolbar: '#barDemo'}
                ]],
                //数据渲染完的回调
                done: function () {
                    //关闭加载
                    layer.closeAll('loading');
                }
            })
        };
        renderTable();

        //监听性别操作
        form.on('switch(test-table-sexDemo)', function (obj) {
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            console.log("==============" + this.value + "==================" + obj.elem.checked + "=======" + obj.othis)
            json = table.clearCacheKey(json);
            console.log(json); //当前行数据
        });

        table.on('toolbar', function (obj) {
            var checkStatus = table.checkStatus('menu');
            var data = checkStatus.data;
            if (obj.event === 'add') {
                if (data.length > 1) {
                    layer.msg("请选择一行作为父级菜单", {offset: 'rb'});
                } else {
                    var id = data.length == 1 ? data[0].id : '';
                    $.get('menu/edit', {parentId: id}, function (data) {
                        layer.open({
                            type: 1,
                            title: '新增',
                            area: ['530px', '550px'],
                            content: data,
                            btn: ['提交', '退出'],
                            success: function (layero, index) {
                                layui.use('form', function () {
                                    var form = layui.form;
                                    layero.addClass('layui-form');
                                    var submitBtn = layero.find('.layui-layer-btn0');
                                    submitBtn.attr('lay-filter', 'formVerify').attr('lay-submit', '');
                                    layero.keydown(function (e) {
                                        if (e.keyCode == 13) {
                                            submitBtn.click();
                                        }
                                    });

                                    form.on('submit(formVerify)', function (data) {
                                        $.post('menu/save', data.field, function (result) {
                                            if (result.success) {
                                                layer.close(index);
                                                //刷新，重新渲染表格
                                                renderTable();
                                            }
                                            layer.msg(result.msg, {offset: 'rb'});
                                        });
                                        return false;
                                    });
                                });
                            }
                        })
                    })
                }
            } else if (obj.event === "updata") {
                if (data.length != 1) {
                    layer.msg("请选择一行进行编辑", {offset: 'rb'});
                } else {
                    var id = data[0].id;
                    $.get('menu/edit', {id: id}, function (data) {
                        layer.open({
                            type: 1,
                            title: '修改',
                            area: ['530px', '550px'],
                            content: data,
                            btn: ['提交', '退出'],
                            success: function (layero, index) {
                                layui.use('form', function () {
                                    var form = layui.form;
                                    layero.addClass('layui-form');
                                    var submitBtn = layero.find('.layui-layer-btn0');
                                    submitBtn.attr('lay-filter', 'formVerify').attr('lay-submit', '');
                                    layero.keydown(function (e) {
                                        if (e.keyCode == 13) {
                                            submitBtn.click();
                                        }
                                    });

                                    form.on('submit(formVerify)', function (data) {
                                        $.post('menu/save', data.field, function (result) {
                                            if (result.success) {
                                                layer.close(index);
                                                //刷新，重新渲染表格
                                                renderTable();
                                            }
                                            layer.msg(result.msg, {offset: 'rb'});
                                        });
                                        return false;
                                    });
                                });
                            }
                        })
                    })
                }
            } else if (obj.event === "delete_lay") {
                if (data.length != 1) {
                    layer.msg("请选择一行进行编辑", {offset: 'rb'});
                } else {
                    var id = data[0].id;
                    layer.confirm('真的删除选定行的数据吗?', function (index) {
                        $.post('menu/delete', {id: id}, function (result) {
                            if (result.success) {
                                layer.close(index);
                                renderTable();
                            }
                            layer.msg(result.msg, {offset: 'rb'});
                        });
                    });
                }
            } else if (obj.event === "refresh_lay") {
                renderTable();
            }
        })
    })
</script>
</body>
</html>